package com.heima.utils.common;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.*;

public class SensitiveWordUtil {

    public static Map<String, Object> dictionaryMap = new HashMap<>();


    /**
     * 生成关键词字典库
     *
     * @param words
     * @return
     */
    public static void initMap(Collection<String> words) {
        if (words == null) {
            System.out.println("敏感词列表不能为空");
            return;
        }

        HashMap<String, Object> map = new HashMap<>(words.size());
        HashMap<String, Object> CurrMap;
        Iterator<String> iterator = words.iterator();
        while (iterator.hasNext()) {
            String word = iterator.next();
            CurrMap = map;
            for (int i = 0; i < word.length(); i++) {
                String key = String.valueOf(word.charAt(i));

                HashMap<String, Object> wordMap = (HashMap<String, Object>) CurrMap.get(key);
                if (wordMap == null) {
                    wordMap = new HashMap<>(2);
                    wordMap.put("isEnd", "0");
                    CurrMap.put(key, wordMap);
                }
                CurrMap = wordMap;
                if (i == word.length() - 1) {
                    CurrMap.put("isEnd", "1");
                }
            }
        }
        dictionaryMap = map;
    }

    /**
     * 搜索文本中某个文字是否匹配关键词
     *
     * @param text
     * @param beginIndex
     * @return
     */
    private static int checkWord(String text, int beginIndex) {
        if (dictionaryMap == null) {
            throw new RuntimeException("字典不能为空");
        }
        boolean isEnd=false;
        Map<String, Object> CurrMap = dictionaryMap;
        int wordLength = 0;
        for (int i = beginIndex; i < text.length(); i++) {
            String word = String.valueOf(text.charAt(i));
            CurrMap = (Map<String, Object>) CurrMap.get(word);
            if (CurrMap == null) {
                break;
            } else {
                wordLength++;
                if ("1".equals(CurrMap.get("isEnd"))) {
                    isEnd = true;
                }

            }
        }
        if(!isEnd){
            wordLength=0;
        }
        return wordLength;
    }

    /**
     * 获取匹配的关键词和命中次数
     *
     * @param text
     * @return
     */
    public static Map<String, Integer> matchWords(String text) {
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < text.length(); i++) {

            int wordLength = checkWord(text, i);
            if (wordLength > 0) {
                String word = text.substring(i, i + wordLength);
                if (map.containsKey(word)) {
                    map.put(word, map.get(word) + 1);
                } else {
                    map.put(word, 1);
                }
                i += wordLength - 1;
            }

        }
        return map;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("法轮");
        list.add("法轮功");
        list.add("冰毒");
        list.add("小");
        initMap(list);
        String content = "我是一个好人，并不会卖冰冰毒毒，也不操练法轮功,我真的不卖冰毒";
        Map<String, Integer> map = matchWords(content);
        System.out.println(map);

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dictionaryMap);
            System.out.println("dictionaryMap:" + json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//        System.out.println(dictionaryMap);
    }
}
